﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Baccarat.CardGameFramework;
using System.Resources;

namespace Baccarat
{
    public partial class BaccaratForm : Form
    {

        private BaccaratGame game = new BaccaratGame(40000);
        private PictureBox[] playerCards;
        private PictureBox[] dealerCards;
        private bool firstTurn;
        private Image m_cardSkin = null;


        public BaccaratForm()
        {
            InitializeComponent();
            LoadCardSkinImages();
            SetUpNewGame();
        }

        public BaccaratGame Game
        {
            get { return game; }
        }

        /// <summary>
        /// Load the Deck Card Images
        /// </summary>
        private void LoadCardSkinImages()
        {
            try
            {
                // Load the card skin image from file

                Bitmap cardSkinBmp = new Bitmap(Properties.Resources.CardGameImageSkinPath);
                m_cardSkin = Image.FromHbitmap(cardSkinBmp.GetHbitmap());
                // Set the deck on the UI to the card skin image
                deckPicBox.Image = m_cardSkin;
            }
            catch (OutOfMemoryException)
            {
                MessageBox.Show("Card skin images are not loading correctly.  Make sure the card skin images are in the correct location.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


            playerCards = new PictureBox[3] { playerCard1, playerCard2, playerCard3 };
            dealerCards = new PictureBox[3] { dealerCard1, dealerCard2, dealerCard3 };
        }

        /// <summary>
        /// Set up the UI for when the game is in play after the player has hit deal game
        /// </summary>
        private void SetUpGameInPlay()
        {
            //tenButton.Enabled = false;
            //twentyFiveButton.Enabled = false;
            //fiftyButton.Enabled = false;
            //hundredButton.Enabled = false;
            //clearBetButton.Enabled = false;
            //standButton.Enabled = true;
            //hitButton.Enabled = true;
            //gameOverTextBox.Hide();
            //playerTotalLabel.Show();
            //dealButton.Enabled = false;

            //if (firstTurn)
            //    doubleDownButton.Enabled = true;
        }

        /// <summary>
        /// Set up the UI for a new game
        /// </summary>
        private void SetUpNewGame()
        {
            //photoPictureBox.ImageLocation = Properties.Settings.Default.PlayerImage;
            //photoPictureBox.Visible = true;
            //playerNameLabel.Text = Properties.Settings.Default.PlayerName;
            //dealButton.Enabled = true;
            //doubleDownButton.Enabled = false;
            //standButton.Enabled = false;
            //hitButton.Enabled = false;
            //clearBetButton.Enabled = true;
            //tenButton.Enabled = true;
            //twentyFiveButton.Enabled = true;
            //fiftyButton.Enabled = true;
            //hundredButton.Enabled = true;
            //gameOverTextBox.Hide();
            //playerTotalLabel.Hide();
            //firstTurn = true;
            //ShowBankValue();
        }

        /// <summary>
        /// Refresh the UI to update the player cards
        /// </summary>
        private void UpdateUIPlayerCards()
        {
            // Update the value of the hand
            //playerTotalLabel.Text = game.CurrentPlayer.Hand.GetSumOfHand().ToString();

            List<Card> pcards = game.CurrentPlayer.Hand.Cards;
            for (int i = 0; i < pcards.Count; i++)
            {
                // Load each card from file
                LoadCard(playerCards[i], pcards[i]);
                playerCards[i].Visible = true;
                playerCards[i].BringToFront();
            }

            List<Card> dcards = game.Dealer.Hand.Cards;
            for (int i = 0; i < dcards.Count; i++)
            {
                LoadCard(dealerCards[i], dcards[i]);
                dealerCards[i].Visible = true;
                dealerCards[i].BringToFront();
            }
        }

        /// <summary>
        /// Takes the card value and loads the corresponding card image from file
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="c"></param>
        private void LoadCard(PictureBox pb, Card c)
        {
            try
            {
                StringBuilder image = new StringBuilder();

                switch (c.Suit)
                {
                    case Suit.Diamonds:
                        image.Append("di");
                        break;
                    case Suit.Hearts:
                        image.Append("he");
                        break;
                    case Suit.Spades:
                        image.Append("sp");
                        break;
                    case Suit.Clubs:
                        image.Append("cl");
                        break;
                }

                switch (c.FaceVal)
                {
                    case FaceValue.Ace:
                        image.Append("1");
                        break;
                    case FaceValue.King:
                        image.Append("k");
                        break;
                    case FaceValue.Queen:
                        image.Append("q");
                        break;
                    case FaceValue.Jack:
                        image.Append("j");
                        break;
                    case FaceValue.Ten:
                        image.Append("10");
                        break;
                    case FaceValue.Nine:
                        image.Append("9");
                        break;
                    case FaceValue.Eight:
                        image.Append("8");
                        break;
                    case FaceValue.Seven:
                        image.Append("7");
                        break;
                    case FaceValue.Six:
                        image.Append("6");
                        break;
                    case FaceValue.Five:
                        image.Append("5");
                        break;
                    case FaceValue.Four:
                        image.Append("4");
                        break;
                    case FaceValue.Three:
                        image.Append("3");
                        break;
                    case FaceValue.Two:
                        image.Append("2");
                        break;
                }

                image.Append(Properties.Resources.CardGameImageExtension);
                String cardGameImagePath = Properties.Resources.CardGameImagePath;
                String cardGameImageSkinPath = Properties.Resources.CardGameImageSkinPath;

                image.Insert(0, cardGameImagePath);
                //check to see if the card should be faced down or up;
                if (!c.IsCardUp)
                    image.Replace(image.ToString(), cardGameImageSkinPath);

                pb.Image = new Bitmap(image.ToString());
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("Card images are not loading correctly.  Make sure all card images are in the right location.");
            }
        }

        private void menuItemStart_Click(object sender, EventArgs e)
        {

            //initialize:
            menuItemStart.Enabled = false;
            menuItemBanco.Enabled = true;
            menuItemCarte.Enabled = true;
            menuItemStay.Enabled = true;

            game.DealNewGame();

            if (null == m_cardSkin)
            {
                return;
            }

            playerCards[2].Image = null;
            dealerCards[2].Image = null;

            LoadCard(playerCards[0], game.CurrentPlayer.Hand.Cards[0]);
            LoadCard(playerCards[1], game.CurrentPlayer.Hand.Cards[1]);
            dealerCards[0].Image = m_cardSkin;
            dealerCards[1].Image = m_cardSkin;

            int currPlayerScore = game.CurrentPlayer.Hand.GetSumOfHand();
            int currDealerScore = game.Dealer.Hand.GetSumOfHand();

            if (game.CurrentPlayer.HasNatural())
            {

                MessageBox.Show("Player has natural.");
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;

            }
            else
            {
                if (game.Dealer.HasNatural())
                {

                    MessageBox.Show("Dealer has natural");
                    menuItemStay.Enabled = false;
                    menuItemCarte.Enabled = false;
                    menuItemBanco.Enabled = false;
                    menuItemStart.Enabled = true;

                }
            }

            if (game.CurrentPlayer.HasNatural() || game.Dealer.HasNatural())
            {
                game.Dealer.Hand.Cards[0].IsCardUp = true;
                game.Dealer.Hand.Cards[1].IsCardUp = true;

                LoadCard(dealerCards[0], game.Dealer.Hand.Cards[0]);
                LoadCard(dealerCards[1], game.Dealer.Hand.Cards[1]);

                //compare score
                if (currPlayerScore > currDealerScore)
                {
                    game.PlayerWin();
                    MessageBox.Show("Player wins");
                    menuItemStay.Enabled = false;
                    menuItemCarte.Enabled = false;
                    menuItemBanco.Enabled = false;
                    menuItemStart.Enabled = true;

                }
                else if (currPlayerScore == currDealerScore)
                {
                    MessageBox.Show("Coup");
                    menuItemStay.Enabled = false;
                    menuItemCarte.Enabled = false;
                    menuItemBanco.Enabled = false;
                    menuItemStart.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Dealer wins");
                    game.PlayerLose();
                    menuItemStay.Enabled = false;
                    menuItemCarte.Enabled = false;
                    menuItemBanco.Enabled = false;
                    menuItemStart.Enabled = true;
                }
            }




        }

        private void menuItemStay_Click(object sender, EventArgs e)
        {

            //Dealer checks against his deck:
            if (game.Dealer.Hand.GetSumOfHand() < 9)
            {
                game.Dealer.Draw();
                LoadCard(dealerCards[2], game.Dealer.Hand.Cards[2]);
            }

            game.Dealer.Hand.Cards[0].IsCardUp = true;
            game.Dealer.Hand.Cards[1].IsCardUp = true;

            LoadCard(dealerCards[0], game.Dealer.Hand.Cards[0]);
            LoadCard(dealerCards[1], game.Dealer.Hand.Cards[1]);


            int currPlayerScore = game.CurrentPlayer.Hand.GetSumOfHand();
            int currDealerScore = game.Dealer.Hand.GetSumOfHand();

            //compare score
            if (currPlayerScore > currDealerScore)
            {
                game.PlayerWin();
                MessageBox.Show("Player wins");
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;

            }
            else if (currPlayerScore == currDealerScore)
            {
                MessageBox.Show("Coup");
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;
            }
            else
            {
                MessageBox.Show("Dealer wins");
                game.PlayerLose();
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;
            }


        }

        private void menuItemCarte_Click(object sender, EventArgs e)
        {
            game.CurrentPlayer.Draw();
            LoadCard(playerCards[2], game.CurrentPlayer.Hand.Cards[2]);

            //Dealer checks against his deck:
            if (game.Dealer.Hand.GetSumOfHand() < 9)
            {
                game.Dealer.Draw();
                LoadCard(dealerCards[2], game.Dealer.Hand.Cards[2]);
            }

            game.Dealer.Hand.Cards[0].IsCardUp = true;
            game.Dealer.Hand.Cards[1].IsCardUp = true;

            LoadCard(dealerCards[0], game.Dealer.Hand.Cards[0]);
            LoadCard(dealerCards[1], game.Dealer.Hand.Cards[1]);


            int currPlayerScore = game.CurrentPlayer.Hand.GetSumOfHand();
            int currDealerScore = game.Dealer.Hand.GetSumOfHand();

            //compare score
            if (currPlayerScore > currDealerScore)
            {
                game.PlayerWin();
                MessageBox.Show("Player wins");
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;

            }
            else if (currPlayerScore == currDealerScore)
            {
                MessageBox.Show("Coup");
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;
            }
            else
            {
                MessageBox.Show("Dealer wins");
                game.PlayerLose();
                menuItemStay.Enabled = false;
                menuItemCarte.Enabled = false;
                menuItemBanco.Enabled = false;
                menuItemStart.Enabled = true;
            }

        }


    }
}